package com.worldcup.service.question.impl;

import com.worldcup.core.ApplicationContextHolder;
import com.worldcup.dao.question.QuestionUserAnswerMapper;
import com.worldcup.model.enums.QuestionTypeEnum;
import com.worldcup.model.question.Question;
import com.worldcup.model.question.QuestionUserAnswer;
import com.worldcup.model.user.UserAddInfo;
import com.worldcup.service.question.QuestionService;
import com.worldcup.service.question.calculator.MatchResultTaskPool;
import com.worldcup.service.user.UserAddInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author duwan.zq
 * @date 2019/03/06
 */
@Service
public class QuestionUserAnswerService {

    @Autowired
    private QuestionService questionService;

    @Autowired
    private QuestionUserAnswerMapper answerMapper;

    @Autowired
    private UserAddInfoService userAddInfoService;

    @Autowired
    private MatchResultTaskPool matchResultTaskPool;

    /**
     * 提交问卷答案
     * @param openid
     * @param answers
     */
    @Transactional(rollbackFor = Exception.class)
    public void submit(String openid, List<QuestionAnswerDTO> answers) {
        //保证事务
        QuestionUserAnswerService service = ApplicationContextHolder.getBean(this.getClass());

        UserAddInfo userAddInfo = new UserAddInfo();
        for(QuestionAnswerDTO answer : answers){
            service.submit(openid, answer.getQuestionId(), answer.getAnswer(), userAddInfo);
        }
        //插入userAddInfo表
        userAddInfoService.save(userAddInfo);

        //异步计算匹配结果信息, 优先级设置最大
        matchResultTaskPool.submitUserTask(openid, Thread.MAX_PRIORITY);
    }

    /**
     * 提交问卷答案
     * @param openid
     * @param questionId
     * @param answer
     */
    @Transactional(rollbackFor = Exception.class)
    public void submit(String openid, long questionId, String answer, UserAddInfo userInfo) {
        Question question = questionService.getQuestion(questionId);
        Assert.notNull(question, "无效的questionId : " + questionId);
        if(!question.getQuestionType().equals(QuestionTypeEnum.USER_INFO.getKey())){
            //非用户信息题直接插入question_user_answer表
            QuestionUserAnswer questionUserAnswer = new QuestionUserAnswer();
            questionUserAnswer.setOpenid(openid);
            questionUserAnswer.setQuestionId(questionId);
            questionUserAnswer.setAnswer(answer);
            questionUserAnswer.setCreatedAt(LocalDateTime.now());
            questionUserAnswer.setUpdatedAt(LocalDateTime.now());
            answerMapper.insert(questionUserAnswer);
        }else{
            //用户信息题构建user_add_info对象
            switch (question.getKeyWord()){
                case "wechat":
                    userInfo.setWechat(answer);
                    break;
                case "gender":
                    userInfo.setGender(Integer.parseInt(answer));
                    break;
                case "city":
                    userInfo.setCity(answer);
                    break;
                case "tag":
                    userInfo.setTag(answer);
                    break;
                case "sign":
                    userInfo.setSign(answer);
                    break;
                default:
                    throw new IllegalArgumentException("未知keyword : " + question.getKeyWord());
            }
        }

    }

    public List<QuestionUserAnswer> getAnswers(String openid) {
        return answerMapper.selectByOpenid(openid);
    }

    public List<String> getAllUser() {
        return answerMapper.getAllUser();
    }

}
